﻿using System;
using System.Collections.Generic;
using System.IO;

namespace ClockwiseClient
{
    public class ClassStockDays
    {
        private List<StockDaysItem> lstStockDays = new List<StockDaysItem>();
        private string symbol;
        public void LoadStockDays(int market, string code)
        {
            string filename;
            switch (market)
            {
                case 0:
                    filename = ClassStock.strWorkDir + @"\wShDays\";
                    symbol = "sh";
                    break;
                default:
                case 1:
                    filename = ClassStock.strWorkDir + @"\wSzDays\";
                    symbol = "sz";
                    break;
                case 2:
                    filename = ClassStock.strWorkDir + @"\wHkDays\";
                    symbol = "hk";
                    break;
                case 3:
                    filename = ClassStock.strWorkDir + @"\wUsDays\";
                    symbol = "us";
                    break;
            }
            try
            {
                filename += code + ".dat";
                symbol += code;
                FileStream fsDays = new FileStream(filename, FileMode.Open);
                lstStockDays.Clear();
                byte[] tick = new byte[GetDaysItemSize()];
                int i, count = (int)(fsDays.Length / GetDaysItemSize());
                for (i = 0; i < count; i++)
                {
                    fsDays.Read(tick, 0, tick.Length);
                    StockDaysItem bt = new StockDaysItem();
                    bt.Convert(tick);
                    lstStockDays.Add(bt);
                }
                fsDays.Close();

            }
            catch (Exception)
            {

            }

        }

        public int GetDaysCount()
        {
            return lstStockDays.Count;
        }

        public StockDaysItem GetDaysItem(int idx)
        {
            if (idx >= 0 && idx < lstStockDays.Count) return lstStockDays[idx];
            return null;
        }

        private int GetDaysItemSize()
        {
            return 32;
        }

        public class StockDaysItem
        {
            public float day = 0;
            public float price = 0;
            public float preclose = 0;
            public float open = 0;
            public float high = 0;
            public float low = 0;
            public float volume = 0;
            public float amount = 0;

            public void Convert(byte[] buff)
            {
                int pos = 0;
                day = BitConverter.ToSingle(buff, pos); pos += 4;
                price = BitConverter.ToSingle(buff, pos); pos += 4;
                preclose = BitConverter.ToSingle(buff, pos); pos += 4;
                open = BitConverter.ToSingle(buff, pos); pos += 4;
                high = BitConverter.ToSingle(buff, pos); pos += 4;
                low = BitConverter.ToSingle(buff, pos); pos += 4;
                volume = BitConverter.ToSingle(buff, pos); pos += 4;
                amount = BitConverter.ToSingle(buff, pos);
            }
        }

        public ClassStockDays findStockDays(string symbol)
        {
            if (symbol.Equals(this.symbol)) return this;
            return null;
        }
    }
}
